<template>
    <el-container>
        <el-header>
            <div><img src="../../pictures/logo.png" width="68px" height="60px"> <span>科记：记录精彩校园生活</span></div>
            <el-menu
            :default-active="activeIndex2"
            class="el-menu-demo"
            mode="horizontal"
            background-color="#99ecff"
            text-color="black"
            active-text-color="#f59456">
                <el-menu-item class="grid-content" index="1">
                    <el-link href="#/home" target="_self" underline="false">科记主页</el-link>
                </el-menu-item>
                <el-submenu class="grid-content" index="3">
                    <template slot="title">生活助手</template>
                    <el-menu-item index="3-1">
                        <router-link :to="{ name: 'blog', params: { id: 1 } }">校园博客</router-link>
                    </el-menu-item>
                    <el-menu-item index="3-2">
                        <router-link :to="{ name: 'blog', params: { id: 2 } }">生活分享</router-link>
                    </el-menu-item>
                    <el-menu-item index="3-3"><a href="#/question" target="_self">校园问答</a></el-menu-item>
                </el-submenu>
                <el-submenu class="grid-content" index="4">
                    <template slot="title">班级事务</template>
                    <el-menu-item index="4-1">
                        <router-link :to="{ name: 'notice'}">班级通知</router-link>
                    </el-menu-item>
                    <el-menu-item index="4-2"><a href="#/account" target="_self">班级账目</a></el-menu-item>
                </el-submenu>
                <el-menu-item class="grid-content" index="5">
                    <el-link href="#/information" target="_self" underline="false">个人信息</el-link>
                </el-menu-item>
                <el-menu-item class="grid-content">
                    <div display: center>
                        <el-input placeholder="请输入内容" v-model="input3" class="input-with-select">
                            <el-select v-model="select" slot="prepend" placeholder="请选择">
                            <el-option label="校园博客" value="1"></el-option>
                            <el-option label="生活助手" value="2"></el-option>
                            <el-option label="校园问答" value="3"></el-option>
                            <el-option label="班级通知" value="4"></el-option>
                            </el-select>
                            <el-button slot="append" icon="el-icon-search"></el-button>
                        </el-input>
                    </div>
                </el-menu-item>
                <el-menu-item>
                    <div><el-col :span="12">
                            <div class="demo-basic--circle">
                                <div class="block"><el-avatar shape="square" :size="50" :src="squareUrl"></el-avatar></div>
                            </div>
                        </el-col> 
                    </div>
                </el-menu-item>
            </el-menu>
            <div class="line"></div>
        </el-header>

        <el-main>
            <div>
            <el-page-header @back="goBack" content="分享内容编辑"></el-page-header></div>
            <br/>
            <span>请选择发布模块</span>
            <br/>
            <br/>
            <el-select v-model="value" placeholder="请选择">
                <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
                </el-option>
            </el-select> 
            <br/>
            <br/>
            <span>请填写标题</span>
            <br/>
            <br/>
            <el-input v-model="title" placeholder="请输入标题"></el-input>
            <br/>
            <br/>
            <div v-if="value === '2'">
                <span>活动名称</span>
                <el-input v-model="eventName" placeholder="请输入活动名称"></el-input>
                <br/>
                <span>金额</span>
                <el-input type="number" v-model="totalCost" placeholder="请输入金额"></el-input>
                <br/>
                <span>经费来源</span>
                <el-input v-model="costSource" placeholder="请输入经费来源"></el-input>    
            </div>
            <br/>
           <MyEditor ref="myEditor"/>
           <div><el-button @click="publishContent" id="button">发布</el-button></div>
        </el-main>
    </el-container>
</template>

<script>

    import { uploadblog } from '@/api/edit.js';
    import MyEditor from './MyEditor.vue';

export default {
    components: { MyEditor },
    data() {
      return {
        activeIndex: '1',
        activeIndex2: '1',//与字体颜色有关的值
        input: '',//搜索栏的输入
        select: '',//搜索栏的选择
        squareUrl: "https://cube.elemecdn.com/9/c2/f0ee8a3c7c9638a54940382568c9dpng.png",//头像的访问路径
        options: [{
          value: '1',
          label: '班级通知'
        }, {
          value: '2',
          label: '班级账单'
        }],
        value: '',
        title: '',
        totalCost: '',
        costSource: ''
      };
    },
    methods: {
        goBack(){
            var targetUrl = "#/home";
            // 使用window.location.href进行页面跳转
            window.location.href = targetUrl;
        },

        publishContent() {
            const id = localStorage.getItem('UserId');
            let url = '';
            let data = {};
            if(this.value == '1'){
                url = `/notices/${id}`;
                data = {
                    title: this.title,
                    content: this.$refs.myEditor.getHtml()
                }
            } else {
                url = `/bills/${id}`;
                data = {
                    eventName: this.title,
                    totalCost: this.totalCost,
                    costSource: this.costSource,
                    content: this.$refs.myEditor.getHtml()
                }
            }
            
            if (this.$refs.myEditor) { 
                uploadblog(url, data).then(response => {
                    if (response.data.code == 1) {
                        alert('发布成功');
                    } else {
                        alert('发布失败');
                    }
                })
            } else {
                console.error("MyEditor 组件未定义");
            }
        }
    }
}
</script>

<style>
    .el-header{
        background: #99ecff;
        display: flex;
        padding-left: 0;
        justify-content: space-between;
        color: black;
        font-size: 20px;
        > div {
            display: flex;
            align-items: center;
            span{
                margin-left: 15px;
            }
            }
    }
    .el-select .el-input {
        width: 130px;
    }
    .input-with-select .el-input-group__prepend {
        background-color: #fff;
    }
    #button {
        background-color: #99ecff;
        color: #f55656;
        padding: 15px 32px;
        float: right;
        text-align: center;
        text-decoration: none;
        display: inline-block;
        font-size: 14px;
    }
</style>